package game.core
{
	import flash.events.Event;
	import flash.utils.getTimer;
	
	/**
	 * 专门来管理
	 * 因为可能需要累加time,所以还是得需要专门的处理一下
	 * 
	 * var gameTimer:GameTimer = new GameTimer(callback);
	 * gameTimer.addTime(123456);
	 */ 
	public class GameTimer
	{
		private var callback:Function;// 执行完之后需要回调用
		
		private var updateFunc:Function;
		
		public function GameTimer(updateFunc:Function,callback:Function)
		{
			this.updateFunc = updateFunc;
			this.callback = callback;
		}
		
		private var totalTime:Number = 0;// 总共的时间
		
		public function addTime(value:Number):void {
			this.totalTime += value;	
		}
		
		private var startTime:Number;
		
		private var _started:Boolean;
		public function start():void {
			if(!_started){
				_started = true;
				startTime = getTimer();
				GameSystem.instance.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
			}
		}
		
		private function enterFrameHandler(event:Event):void {
			var diffTime:Number = getTimer() - startTime;
			this.totalTime -= diffTime;
			this.updateFunc.apply(null,[this.totalTime]);
			if(this.totalTime <= 0){
				this._started = false;
				this.updateFunc = null;
				GameSystem.instance.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
				this.callback();
			}
		}
	}
}